define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ad/index',
                    add_url: 'ad/add',
                    edit_url: 'ad/edit',
                    del_url: 'ad/del',
                    multi_url: 'ad/multi',
                    table: 'ad',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'ad_id',
                sortName: 'ad_id',
                columns: [
                    [
                        {field: 'ad_title', title: __('Ad_title'), operate: false},
                        {field: 'ad_subtitle', title: __('Ad_subtitle'),operate: 'LIKE'},
                        {field: 'jump_type', title: __('Jump_type'),visible: false, operate: false},
                        {field: 'jump_type_text', title: __('Jump_type'), operate: false},
                        {field: 'ad_link', title: __('Ad_link'),formatter: Controller.api.formatter.url, operate: false},
                        {field: 'sort', title: __('Sort'), operate: false},
                        {field: 'starttime', title: __('Starttime'), operate: false, addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate: false, addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'),visible: false, searchList: {0: __('Status 0'), 1: __('Status 1'), 10: __('Status 10')}},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="example/bootstraptable/change" data-id="' + row.id + '"><i class="fa ' + (row.title == '' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});